#!/bin/bash
set -eu

TESTDIR=$(readlink -f $(dirname $0))
. $TESTDIR/framework

setupenvironment

buildpackage 'conflicting-package-distupgrade'
buildpackage 'conflicting-package-one'
buildpackage 'conflicting-package-two'
buildpackage 'missing-dependency'
buildpackage 'simple-package-new'
buildpackage 'simple-package-noarch'
buildpackage 'simple-package'
buildpackage 'simple-package-update-conflict'
buildpackage 'simple-package-update' # overwrites the previous built package

generaterepository_and_switch_sources "$TMPWORKINGDIRECTORY/usr/src/RPM/RPMS"

testsuccess aptget update

# useful regexes:
readonly NL=$'\n'
readonly ANY="[^$NL]"

# values occurring many times in the dump:

set_regex_quote_str APT_SOURCES_SITE_RE "$APT_SOURCES_SITE"
readonly APT_SOURCES_SITE_RE

set_regex_quote_str DISTRO_COMPONENT_RE "$DISTRO_COMPONENT"
readonly DISTRO_COMPONENT_RE

set_regex_quote_str APT_FETCHED_NOARCH_PKGLIST_RE "$APT_FETCHED_NOARCH_PKGLIST"
readonly APT_FETCHED_NOARCH_PKGLIST_RE
set_regex_quote_str APT_FETCHED_MYARCH_PKGLIST_RE "$APT_FETCHED_MYARCH_PKGLIST"
readonly APT_FETCHED_MYARCH_PKGLIST_RE

readonly -a dump_pkgs=(
	"Package: conflicting-package-two
 Version: 1-alt1@[0-9]+
     File: $APT_FETCHED_MYARCH_PKGLIST_RE
  Depends: conflicting-package-one \(null\)"

	"Package: no-such-package"

	"Package: simple-package-noarch
 Version: 1-alt1@[0-9]+
     File: $APT_FETCHED_NOARCH_PKGLIST_RE"

	"Package: simple-package-new
 Version: 3-alt1@[0-9]+
     File: $APT_FETCHED_MYARCH_PKGLIST_RE
  Depends: simple-package 3-alt1
  Depends: simple-package 3-alt1"

	"Package: missing-dependency
 Version: 1-alt1@[0-9]+
     File: $APT_FETCHED_MYARCH_PKGLIST_RE
  Depends: no-such-package \(null\)"

	"Package: simple-package
 Version: 2-alt1@[0-9]+
     File: $APT_FETCHED_MYARCH_PKGLIST_RE
 Version: 1-alt1@[0-9]+
     File: $APT_FETCHED_MYARCH_PKGLIST_RE"

	"Package: conflicting-package-one
 Version: 2-alt1@[0-9]+
     File: $APT_FETCHED_MYARCH_PKGLIST_RE
  Depends: conflicting-package-two \(null\)
  Depends: simple-package \(null\)
 Version: 1-alt1@[0-9]+
     File: $APT_FETCHED_MYARCH_PKGLIST_RE
  Depends: conflicting-package-two \(null\)"

)

if [ -n "$APT_TEST_GPGPUBKEY" ]; then
	dump_with_gpgpubkey=(
		"Package: gpg\($ANY*@$ANY*\)"

		"Package: gpg\($ANY*\)"

		"Package: gpg-pubkey
 Version: [^-$NL]*-[^-$NL]*@[0-9]+
     File: /$ANY*/var/lib/rpm/Packages"

	)
else
	dump_with_gpgpubkey=()
fi
readonly -a dump_with_gpgpubkey

readonly dump_rpmdb="File: /$ANY*/var/lib/rpm/Packages
 Type: RPM Database
 Size: [0-9]+
 ID: 2
 Flags: 1
 Time: $ANY*
 Archive: \(null\)
 Component: \(null\)
 Version: \(null\)
 Origin: \(null\)
 Site:[ ]
 Label: \(null\)
 Architecture: \(null\)"

case "$APT_TEST_METHOD" in
	cdrom_missing_release)
		readonly -a dump_lists=(
			"File: $APT_FETCHED_NOARCH_PKGLIST_RE
 Type: RPM Package Index
 Size: [0-9]+
 ID: [01]
 Flags: 0
 Time: $ANY*
 Archive: \(null\)
 Component: \(null\)
 Version: \(null\)
 Origin: \(null\)
 Site: $APT_SOURCES_SITE_RE
 Label: \(null\)
 Architecture: \(null\)"

			"File: $APT_FETCHED_MYARCH_PKGLIST_RE
 Type: RPM Package Index
 Size: [0-9]+
 ID: [01]
 Flags: 0
 Time: $ANY*
 Archive: \(null\)
 Component: \(null\)
 Version: \(null\)
 Origin: \(null\)
 Site: $APT_SOURCES_SITE_RE
 Label: \(null\)
 Architecture: \(null\)"

		)
		;;
	*)
		readonly -a dump_lists=(
			"File: $APT_FETCHED_NOARCH_PKGLIST_RE
 Type: RPM Package Index
 Size: [0-9]+
 ID: [01]
 Flags: 0
 Time: $ANY*
 Archive: ALT Linux $DISTRO_COMPONENT_RE
 Component: $DISTRO_COMPONENT_RE
 Version: [0-9]+
 Origin: ALT Linux Team
 Site: $APT_SOURCES_SITE_RE
 Label: $DISTRO_COMPONENT_RE
 Architecture: noarch"

			"File: $APT_FETCHED_MYARCH_PKGLIST_RE
 Type: RPM Package Index
 Size: [0-9]+
 ID: [01]
 Flags: 0
 Time: $ANY*
 Archive: ALT Linux $DISTRO_COMPONENT_RE
 Component: $DISTRO_COMPONENT_RE
 Version: [0-9]+
 Origin: ALT Linux Team
 Site: $APT_SOURCES_SITE_RE
 Label: $DISTRO_COMPONENT_RE
 Architecture: $APT_TEST_USED_ARCH"

		)
		;;
esac

testpartsexhaust 'Using Versioning System: Standard \.rpm' \
				 "${dump_pkgs[@]}" \
				 "${dump_with_gpgpubkey[@]}" \
				 "$dump_rpmdb" \
				 "${dump_lists[@]}" \
-- aptcache dump
